package com.zhugang.week05;

import java.util.HashMap;
import java.util.Map;

/**
 * @program algorithms
 * @description: singleNumber
 * @author: chanzhugang
 * @create: 2022/07/08 18:04
 */
public class SingleNumber {

    public static void main(String[] args) {
        SingleNumber singleNumber = new SingleNumber();
        int[] nums = new int[]{4, 1, 2, 1, 2};
        int res = singleNumber.singleNumber(nums);
        System.out.println(res);
    }

    /**
     * 136 只出现一次的数字
     *
     * @param nums
     * @return
     */
    public int singleNumber(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int num : nums) {
            int count = 1;
            if (map.containsKey(num)) {
                count += map.get(num);
            }
            map.put(num, count);
        }

        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                return entry.getKey();
            }
        }
        return -1;
    }
}